package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.DIYException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-21
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    /**
     * 登录  返回token
     * @param member
     * @return
     */
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码任意为空，则无法登录
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new DIYException(20001,"登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //根据手机号查询对象
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);

        //判断查询对象是否为空
        if (mobileMember==null){
            throw new DIYException(20001,"该账号还未注册，请前往注册");
        }

        //判断查询对象密码
        //存储到数据库的密码进行加密了
        //把输入的密码需要先进行MD5加密，再比较
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new DIYException(20001,"密码错误");
        }

        //判断用户是否被禁用
        if (mobileMember.getIsDisabled()){
            throw new DIYException(20001,"该用户被禁用，请联系管理员");
        }

        //登录成功，生成token字符串，JWT
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return token;
    }

    /**
     * 注册的方法
     * @param registerVo
     */
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
    //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile();  //手机
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        //非空判断
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(code)||
                StringUtils.isEmpty(nickname)){
            throw new DIYException(20001,"登录失败");
        }
        //判断手机验证码
        //先获取redis里的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new DIYException(20001,"注册失败");
        }

        //判断手机号是否重复，表里面存在相同手机号不能进行添加
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //查询数据库里有无重复
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new DIYException(20001,"账号已存在");
        }
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        member.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/65423f14-49a9-4092-baf5-6d0ef9686a85.png");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
